﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetDon.OA.Interface
{
    public static class SystemUtility
    {

        public static int GetDelayedDays(DateTime? plannedShippingDate, DateTime? actualShippingDate)
        {
            return plannedShippingDate.HasValue && actualShippingDate.HasValue
                       ? GetDelayedDays(plannedShippingDate.Value, actualShippingDate.Value)
                       : 0;
        }

        public static int GetDelayedDays(DateTime plannedShippingDate, DateTime actualShippingDate)
        {
            if (plannedShippingDate.Date == DateTime.MinValue.Date || actualShippingDate.Date == DateTime.MinValue.Date)
                return 0;
            return actualShippingDate.Subtract(plannedShippingDate).Days;
        }


        public static decimal GetGoodsLeftAmount(decimal? orderQuantity, int? actualEntruckingBoxAmount)
        {
            if (orderQuantity.HasValue && actualEntruckingBoxAmount.HasValue)
            {
                return orderQuantity.Value - actualEntruckingBoxAmount.Value;
            }
            if (orderQuantity.HasValue)
                return orderQuantity.Value;
            return 0;
        }

        public static decimal? GetUnitCost(decimal? shuntingCost, decimal? actualEntruckingAmount)
        {
            if (shuntingCost.HasValue && actualEntruckingAmount.HasValue)
            {
                return Math.Round(shuntingCost.Value / actualEntruckingAmount.Value);
            }
            if (shuntingCost.HasValue)
            {
                return Math.Round(shuntingCost.Value);
            }
            return 0;
        }

        public static decimal GetShuntingCost(IEnumerable<IssuingOrderDeliveryData> orderDeliveries, bool isCalculatedByWeight)
        {
            decimal? total = 0;
            foreach (var issuingOrderDeliveryData in orderDeliveries)
            {
                total += (isCalculatedByWeight
                             ? issuingOrderDeliveryData.OrderWeight
                             : issuingOrderDeliveryData.OrderVolumn) * issuingOrderDeliveryData.UnitCost;
            }
            return total ?? 0;
        }

    }
}
